﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Npgsql;

namespace IonosphereViewer
{
    public partial class WebFormDefault : System.Web.UI.Page
    {
        // PostgeSQL-style connection string
        public static string constring = String.Format("Server={0};Port={1};" +
               "User Id={2};Password={3};Database={4};",
               ConfigurationManager.AppSettings["host"],
               ConfigurationManager.AppSettings["port"],
               ConfigurationManager.AppSettings["user"],
               ConfigurationManager.AppSettings["ps"],
               ConfigurationManager.AppSettings["db"]);
        //public NpgsqlConnection conn = new NpgsqlConnection(WebFormDefault.constring);

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Error += new EventHandler(_Default_Error);
        }

        void _Default_Error(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Write("Page Error!<br />");
            Response.Write(string.Format("Error: <b>{0}</b>", Server.GetLastError().Message));
            Server.ClearError();
        }

        protected void LoginLog_Authenticate(object sender, EventArgs e)
        {
            /*/try
            {
                conn.Open();
                try
                {
                    NpgsqlCommand dccard = new NpgsqlCommand("SELECT card_no, group_no FROM cardinfo WHERE card_no = @card_no AND holder = @holder", conn);
                    NpgsqlParameter param1 = new NpgsqlParameter();
                    param1.ParameterName = "card_no";
                    param1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer;
                    param1.Value = int.Parse(TextBoxCardNo.Text);
                    NpgsqlParameter param2 = new NpgsqlParameter();
                    param2.ParameterName = "holder";
                    param2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Char;
                    param2.Size = 40;
                    param2.Value = TextBoxHolder.Text.ToString().Trim();
                    dccard.Parameters.Add(param1);
                    dccard.Parameters.Add(param2);
                    NpgsqlDataReader er = dccard.ExecuteReader();
                    try
                    {
                        if (er.HasRows)
                        {
                            er.Read();
                            Session["Card_No"] = er["card_no"];
                            Session["Group_No"] = er["group_no"];
                            //Response.Redirect("~/ClientInfo.aspx");/*/
                            Server.Transfer("~/ClientInfo.aspx");
            /*/}
            else
            {
                CustomValidator1.IsValid = false;
            }
        }
        finally
        {
            er.Close();
        }
    }
    finally
    {
        conn.Close();
    }
}
catch
{
    CustomValidator1.IsValid = false;
}/*/
        }
    }
}
